﻿namespace PlugNT.Pack.Imager
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    public class SimpleWaterMark
    {
        private Stream _imgstream;
        private string _savepath;
        private string _waterimagepath;
        private string _watertext;

        public string SaveImageWater()
        {
            if ((this.WaterImagePath == null) || (this.WaterImagePath == ""))
            {
                return "水印的图片路径不能为空！";
            }
            if (!File.Exists(this.WaterImagePath))
            {
                return "水印的图片路径错误！";
            }
            if ((this.SavePath == null) || (this.SavePath == ""))
            {
                return "保存图片的路径不能为空！";
            }
            if (this.ImgStream == null)
            {
                return "水印图片流对象不能为空！";
            }
            try
            {
                Image tmpimage = Image.FromStream(this.ImgStream);
                Image watermark = Image.FromFile(this.WaterImagePath);
                if ((watermark.Height >= tmpimage.Height) || (watermark.Width >= tmpimage.Width))
                {
                    return "水印图片宽度和高度不能大于上传图片的值！";
                }
                Graphics g = Graphics.FromImage(tmpimage);
                g.DrawImage(watermark, new Rectangle(tmpimage.Width - watermark.Width, tmpimage.Height - watermark.Height, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
                tmpimage.Save(this.SavePath, ImageFormat.Jpeg);
                tmpimage.Dispose();
                g.Dispose();
                watermark.Dispose();
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string SaveStringWater()
        {
            if ((this.SavePath == null) || (this.SavePath == ""))
            {
                return "保存图片的路径不能为空！";
            }
            if (this.ImgStream == null)
            {
                return "水印图片流对象不能为空！";
            }
            if (this.WaterText == null)
            {
                return "水印文字不能为空！";
            }
            try
            {
                Image tmpimage = Image.FromStream(this.ImgStream);
                Image watermark = Image.FromFile(this.WaterImagePath);
                if ((watermark.Height >= tmpimage.Height) || (watermark.Width >= tmpimage.Width))
                {
                    return "水印图片宽度和高度不能大于上传图片的值！";
                }
                Graphics g = Graphics.FromImage(tmpimage);
                Font drawFont = new Font("宋体", 20f);
                g.DrawString(this.WaterText, drawFont, new SolidBrush(Color.White), (float) (tmpimage.Width - watermark.Width), (float) (tmpimage.Height - watermark.Height));
                tmpimage.Save(this.SavePath, ImageFormat.Jpeg);
                tmpimage.Dispose();
                g.Dispose();
                watermark.Dispose();
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public Stream ImgStream
        {
            get
            {
                return this._imgstream;
            }
            set
            {
                this._imgstream = value;
            }
        }

        public string SavePath
        {
            get
            {
                return this._savepath;
            }
            set
            {
                this._savepath = value;
            }
        }

        public string WaterImagePath
        {
            get
            {
                return this._waterimagepath;
            }
            set
            {
                this._waterimagepath = value;
            }
        }

        public string WaterText
        {
            get
            {
                return this._watertext;
            }
            set
            {
                this._watertext = value;
            }
        }
    }
}

